package com.hzw.saas.web.admin.email.controller;

import java.util.List;

import com.hzw.saas.api.template.ITemplateService;
import com.hzw.saas.api.template.bo.TemplateBO;
import com.hzw.saas.api.template.bo.TemplateTypeBO;
import com.hzw.saas.api.template.constants.TemplateApiConsts;
import com.hzw.saas.api.template.query.TemplateAddQuery;
import com.hzw.saas.api.template.query.TemplateUpdateQuery;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.web.admin.email.params.IdsParam;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zzl
 * @since 09/09/2021
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@Api(tags = "模板管理")
public class TemplateController {

    private final ITemplateService templateService;


    @GetMapping("/sys/template/types")
    @SysLog
    @ApiOperation(value = "获取模板类型列表", notes = "获取模板类型列表， 含每个类型可以使用的变量")
    @ApiOperationSort(11)
    public ResponseEntity<List<TemplateTypeBO>> listTemplateTypes() {
        List<TemplateTypeBO> bos = templateService.listTemplateTypes();
        return ResponseEntity.ok(bos);
    }


    @GetMapping("/sys/templates")
    @SysLog
    @ApiOperation(value = "获取模板列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "type", value = "模板类型, " + TemplateApiConsts.TEMPLATE_TYPE, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "key", value = "模板关键字", dataType = "String", paramType = "query"),
    })
    @ApiOperationSort(12)
    public ResponseEntity<List<TemplateBO>> listTemplates(Integer type, String key) {
        List<TemplateBO> bos = templateService.listTemplates(type, key);
        return ResponseEntity.ok(bos);
    }

    @GetMapping("/sys/template/{pid}")
    @SysLog
    @ApiOperation(value = "获取模板")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pid", value = "模板PID", dataType = "String", paramType = "path", required = true)
    })
    @ApiOperationSort(12)
    public ResponseEntity<TemplateBO> getTemplate(@PathVariable String pid) {
        TemplateBO template = templateService.getTemplate(pid);
        return ResponseEntity.ok(template);
    }

    @GetMapping("/sys/template/{pid}/biz")
    @SysLog
    @ApiOperation(value = "模板转换业务内容", notes = "根据模板定义的业务功能，获取替换业务变量后的模板内容")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pid", value = "模板PID", dataType = "String", paramType = "path", required = true),
        @ApiImplicitParam(name = "params", value = "业务参数; " + TemplateApiConsts.TEMPLATE_PARAMS, dataType = "String", allowMultiple = true, paramType = "query", required = false),
    })
    @ApiOperationSort(12)
    public ResponseEntity<TemplateBO> convertTemplate(@PathVariable String pid, @RequestParam("params") String[] params) {
        TemplateBO bo = templateService.convertTemplate(pid, params);
        return ResponseEntity.ok(bo);
    }

    @PostMapping("/sys/template")
    @SysLog(operation = "新增模板", mode = 3)
    @ApiOperation(value = "新增模板")
    @ApiOperationSort(13)
    public ResponseEntity<Void> addTemplate(@Validated @RequestBody TemplateAddQuery query) {
        templateService.addTemplate(query);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/sys/template")
    @SysLog(operation = "更新模板", mode = 3)
    @ApiOperation(value = "更新模板")
    @ApiOperationSort(14)
    public ResponseEntity<Void> updateTemplate(@Validated @RequestBody TemplateUpdateQuery query) {
        templateService.updateTemplate(query);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/sys/template/{pid}")
    @SysLog(operation = "删除模板", mode = 3)
    @ApiOperation(value = "删除模板")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pid", value = "模板PID", dataType = "String", paramType = "path", required = true)
    })
    @ApiOperationSort(15)
    public ResponseEntity<Void> deleteTemplate(@PathVariable String pid) {
        templateService.deleteTemplate(pid);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/sys/templates")
    @SysLog(operation = "批量删除模板", mode = 3)
    @ApiOperation(value = "批量删除模板")
    @ApiOperationSort(16)
    public ResponseEntity<Void> deleteTemplates(@RequestBody IdsParam param) {
        templateService.deleteTemplate(param.getPids());
        return ResponseEntity.ok().build();
    }

}
